package com.baiyangtx.lingerweather.net;

import java.util.HashMap;
import java.util.Map;

import com.baiyangtx.lingerweather.adapt.CityKey;

public class ForecastParser implements Parser{

	
	private String text ;
	
	
	private Map<String , String> city ;

	
	

	
	@Override
	public void setText(String text) {
		this.text = text ;
	}
	
	@Override
	public Map<String, String> parser() {
		if(city == null ){
			city =  new HashMap<String , String>(); ;
			Map<String , String > parse = parseToPair() ;
			parserFeeling(parse) ;
//			parseWind(parse) ;
			parserTemperature(parse) ;
			parseWeather(parse) ;
		}
		return city ;
	}
	
	
	private Map<String , String> parseToPair(){
		
		Map<String , String> map = new HashMap<String, String>() ;
		String[] keyValue = text.split(",") ;
		for(int i = 0 ; i < keyValue.length ; i++)
		{
			String[] pair = keyValue[i].replaceAll("\"", "").split(":") ;
			if( pair.length > 1 ){
				map.put(pair[0], pair[1]) ;
				System.out.println(pair[0] + ":" + pair[1]);
			}else{
				map.put(pair[0], "null");
			}
			
		}
		return map ;
	}
	
//	private void parseWind(Map<String , String > map){
//		
//		String windKey = "wind" ;
//		String powerKey = "fl1" ;
//		
//		String wind = map.get(windKey) ;
//		String power = map.get(powerKey) ;
//		city.put( CityKey.WIND ,  wind ) ;
//		city.put( CityKey.WIND_POWER ,  power ) ;
//	}
//	
	
	
	
	
	private void parserFeeling(Map<String , String > map){
		
		String feelKey = "index" ;
		String feelDescKey = "index_d" ;
		
		String feel = map.get(feelKey) ;
		String feelDesc = map.get(feelDescKey) ;
		city.put( CityKey.FEELING ,  feel ) ;
		city.put( CityKey.FEELING_DESCRIPTION ,  feelDesc ) ;
	}
	
	
	
	
	private void parserTemperature(Map<String , String > map ){
		String base = "temp" ;
		StringBuilder hTemp = new StringBuilder() ;
		StringBuilder lTemp = new StringBuilder() ;
		for( int i = 1  ; i < 7 ; i++ ){
			String temp = map.get(base+i) ;
			String[] temperature = temp.replaceAll("℃", "").split("~") ;
			int hight = Integer.valueOf(temperature[0]) ;
			int low = Integer.valueOf(temperature[1]) ;
			if( low > hight) {
				int t = hight ;
				hight = low ;
				low = t ;
			}
			hTemp.append(hight).append( CityKey.SPIT ) ;
			lTemp.append(low).append( CityKey.SPIT ) ;
		}
		hTemp.deleteCharAt(hTemp.length()-1) ;
		lTemp.deleteCharAt(lTemp.length()-1) ;
		city.put( CityKey.FUTURE_TEMP_HIGHT , hTemp.toString() ) ;
		city.put( CityKey.FUTURE_TEMP_LOW , lTemp.toString() ) ;
	}
	
	
	private void parseWeather(Map<String , String > map){
		String base = "img_title" ;
		StringBuilder noonWeather = new StringBuilder() ;
		StringBuilder nightWeather = new StringBuilder() ;
		for( int i = 1 ; i <= 12 ; i+=2 ){
			map.get(base + i ) ;
			String noon = parseToLingerWeather( map.get(base + i ) );
			String night = parseToLingerWeather( map.get(base+(i+1)) ) ;
			noonWeather.append(noon).append(CityKey.SPIT) ;
			nightWeather.append(night).append(CityKey.SPIT) ;
			if( i == 1 ){
				city.put( CityKey.RECENT_WEATHER , noon) ;
			}
		}
		noonWeather.deleteCharAt( noonWeather.length() - 1 ) ;
		nightWeather.deleteCharAt(nightWeather.length() - 1 ) ;
		city.put( CityKey.FUTURE_WEATHER_NOON ,  noonWeather.toString() ) ;
		city.put( CityKey.FUTURE_WEATHER_NIGHT, nightWeather.toString()) ;
		
	}

	private String parseToLingerWeather(String weather){
		String base = null ;
		if(	weather.contains("晴") ){
			base = CityKey.WEATHER_SUNNY ;
		}else if( weather.contains("云") ){
			base = CityKey.WEATHER_CLOUDY ;
		}else if ( weather.contains("阴") ){
			base = CityKey.WEATHER_OVERCAST ;
		}else if( weather.contains("风") ){
			base = CityKey.WEATHER_WINDY ;
		}else if( weather.contains("雪")) {
			if( weather.contains("小") || weather.contains("雨") ){
				base = CityKey.WEATHER_LIGHT_SNOW ;
			}else{
				base = CityKey.WEATHER_HEAVY_SNOW ;
			}
		}else if( weather.contains("雨") ){
			if( weather.contains("小雨") ){
				base = CityKey.WEATHER_LIGHT_RAIN ;
			}else if( weather.contains("暴") ){
				base = CityKey.WEATHER_HARD_RAIN ;
			}else{
				base = CityKey.WEATHER_HEAVY_RAIN ;
			}
		}else if( weather.contains("雾") ){
			base = CityKey.WEATHER_FOG ;
		}
		return base ;
	}
	
	public static void main(String[] s){
		System.out.println( "temp6".charAt(4)); 
	}

	
}
